<script lang="ts" setup>
import { useRoute } from 'vue-router'
import axios from '@/request/request'
import { onBeforeMount, ref, watch } from 'vue'
import type { Course, Result } from '@/request/types'
import { useTabName } from '@/hook/tab'
import router from '@/router'
import CourseQA from '@/components/CourseQA.vue'
import CourseTask from '@/components/CourseTask.vue'
import ProgressLeaderboard from '@/components/ProgressLeaderboard.vue'

const route = useRoute()
const course = ref<Course>()

const requestCourse = async () => {
  const result = await axios.get<Result<Course>>(`/api/curriculum/${route.params.id}`)
  course.value = result.data.data
  console.log(result.data)
}

onBeforeMount(async () => {
  await requestCourse()
  useTabName(course.value?.name)
})

watch(router.currentRoute, async () => {
  if (router.currentRoute.value.name === 'course-learn') {
    await requestCourse()
    useTabName(course.value?.name)
  }
})

const activeTab = ref('task')
</script>

<template>
  <div>
    <!--  头部  -->
    <div
      class="w-full p-8 bg-cover bg-center bg-no-repeat bg-[url('http://test.codelover.club/static/img/courseBg.43bcf71c.png')]"
    >
      <div class="container mx-auto flex items-center justify-between h-full flex-wrap gap-8">
        <!--   左侧     -->
        <div class="text-white flex flex-col gap-6">
          <div class="text-3xl">
            {{ course?.name }}
            <span class="text-xl">(已选{{ course?.choosedCount }}人)</span>
          </div>
          <div class="flex gap-4 items-center">
            <el-avatar :src="course?.iconPath" size="large" />
            <div class="flex flex-col">
              <span class="font-black">{{ course?.mainDesignerUserName }}</span>
              <el-tag effect="dark" type="warning">
                <span class="text-sm">教师</span>
              </el-tag>
            </div>
            <el-button class="ml-8" type="primary">课程成绩</el-button>
          </div>
        </div>

        <!--   右侧     -->
        <el-card body-class="flex flex-col gap-4">
          <el-button round size="large" type="warning">开始学习</el-button>
          <span class="text-sm font-black">您还未选课学习，快来和我们一起学习吧！</span>
        </el-card>
      </div>
    </div>
    <!--  内容  -->
    <div class="container mx-auto">
      <el-tabs v-model="activeTab" type="card">
        <el-tab-pane label="任务" name="task">
          <CourseTask v-if="course" :course="course" />
        </el-tab-pane>
        <el-tab-pane label="问答" name="question">
          <CourseQA />
        </el-tab-pane>
        <el-tab-pane label="进度榜" name="progress-rank">
          <ProgressLeaderboard />
        </el-tab-pane>
        <el-tab-pane label="投票榜" name="voting-list"> 投票榜</el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<style scoped>
:deep(.el-card) {
  --el-card-border-radius: 0.75em;
}
</style>
